layui.define(['jquery','table','layer','eleTree','form','common'], function(exports) {
    var $ = layui.jquery,
        layer = layui.layer,
        eleTree = layui.eleTree,
        form = layui.form,
        common = layui.common,
        table = layui.table;

    var tree_cur_deptId,tree_cur_pid;
    var tableRole = null;
    var deptTree=null;
    //页面事件
    var active = {
        reload: function(){
            var loginName = $('#filter_loginName');
            var phonenumber = $("#filter_phonenumber");
            var deptId = tree_cur_deptId;
            var parentId = tree_cur_pid;

            //执行重载
            table.reload('table_user', {
                page: {
                    curr: 1
                }
                ,where: {
                    "loginName": loginName.val(),
                    "phonenumber": phonenumber.val(),
                    "deptId": deptId,
                    "parentId": parentId
                }
            });
        }
    };

    var _config = null;
    var view = {
        init: function (config) {
            _config = config;
            this.initTree();
            this.initTable();
            this.initForm();
        },
        initTable: function () {
            tableRole = table.render({
                method: 'POST',
                url: prefix + "/list", //数据接口
                toolbar: '#table_user_toolbar',
                done: function () {
                    $('#table_user').next().css('height', 'auto');
                },
                limit: 20,
                elem: '#table_user',
                height: 680,
                page: true,
                cols: new Array([{
                    field: 'loginName',
                    title: '登录名称',
                    sort: true,
                    align: 'center',
                    width: 100,
                    fixed: 'left'
                },
                    {
                        field: 'userName',
                        sort: true,
                        width: 100,
                        align: 'center',
                        title: '用户名称'
                    },
                    {
                        field: 'dept.deptName',
                        sort: true,
                        align: 'center',
                        title: '部门'
                    },
                    {
                        field: 'email',
                        title: '邮箱',
                        sort: true,
                        align: 'center',
                        visible: false
                    },
                    {
                        field: 'phonenumber',
                        sort: true,
                        align: 'center',
                        title: '手机'
                    },
                    {
                        // visible: editFlag == 'hidden' ? false : true,
                        title: '冻结账户',
                        sort: true,
                        width: 100,
                        align: 'center',
                        templet: '#tmplUserStatus'
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        sort: true
                    },
                    {
                        title: '操作',
                        align: 'center',
                        toolbar: '#col_operation'
                    }])
            });

            //监听工具栏
            table.on('toolbar(tableUser)', function(obj){
                switch(obj.event){
                    case 'createUser':
                        layer.open({
                            type: 2,
                            title: '新增用户',
                            shade: false,
                            fixed: false,
                            maxmin: true,
                            area: ['600px', '450px'],
                            content: prefix + '/add',
                            btn:["创建","取消"],
                            yes:function(index,layero){
                                layui.config({
                                    base: '/js/system/user/'
                                }).use('add',function(){
                                    layui.add.submit({
                                        "index":index,
                                        "layero":layero
                                    });
                                });
                            }
                        });
                        break;
                }
            });

            //监听事件
            table.on('tool(tableUser)', function(obj){
                switch(obj.event){
                    case 'del':
                        common.ajaxRemove("/system/user/remove",obj.data.id,function(res){
                            active['reload'].call(this);
                        });
                        break;
                    case 'reset':
                        var url = '/system/user/resetPwd';
                        layer.confirm('确定重置该用户密码为123456么？', function(index) {
                            common.ajax.post(url, {
                                id: obj.data.id,
                                loginName: obj.data.loginName,
                                password: "123456"
                            }, function (res) {
                                if(res.code  == 0){
                                    layer.alert("重置密码操作成功！");
                                }else{
                                    layer.alert("重置密码操作失败！");
                                }
                                layer.close(index);
                            });
                        });
                        break;
                    case 'edit':
                        layer.open({
                            type: 2,
                            title: '编辑',
                            shade: false,
                            fixed: false,
                            maxmin: true,
                            area: ['800px', '600px'],
                            content: prefix + '/edit/'+obj.data.id,
                            btn:["保存","取消"],
                            yes:function(index,layero){
                                layui.config({
                                    base: '/js/system/user/'
                                }).use('add',function(){
                                    layui.add.submit({
                                        "index":index,
                                        "layero":layero
                                    });
                                });
                            }
                        });

                        break;
                }
            });

            //监听表格复选框选择
            table.on('radio(tableUser)', function (obj) {
                console.log(obj,this.value)
            });

            form.on('switch(filter-switchUserStatus)',function(obj){
                common.ajax.post(prefix +'/changeStatus',{
                    id:this.value,
                    status:obj.elem.checked?0:1
                },function(res){
                    layer.msg(res.msg);
                    // console.log(res);
                });
            });

        },
        initTree: function(){
            deptTree=eleTree.render({
                    elem: '.ele1',
                    url: "/system/dept/treeData",
                    request:{
                        name: "name",
                        key: "id",
                        children: "children",
                        checked: "checked",
                        disabled: "disabled",
                        isLeaf: "isLeaf"
                    },
                    response: {
                        statusName: "code",
                        statusCode: 0,
                        dataName: "data"
                    },
                    defaultExpandAll: true,
                    checkOnClickNode:true,
                    //defaultExpandedKeys:[100],
                    renderAfterExpand: true
                }
            );
            eleTree.on("nodeClick(treeDept)",function(d) {
                //if(d.data.currentData.isLeaf){
                tree_cur_deptId = d.data.currentData.id;
                tree_cur_pid = d.data.currentData.pId;
                active['reload'].call(this);
                //}
            });

        },
        initForm: function () {
            form.render();
            $('form.layui-form .layui-btn').on('click', function (e) {
                console.log("btn click",e)
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
                return false;
            });
        },
        removeItems: function () {
            active['remove'].call(this);
        },
        refreshTable: function () {
            active['reload'].call(this);
        }
    }



    exports('list',view);

})
